﻿using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;

namespace JESAI.Core.Logging
{
    /// <summary>
    /// 服务集合扩展
    /// </summary>
    public static class ServiceCollectionExtensions
    {
        /// <summary>
        /// 注册日志
        /// </summary>
        /// <param name="services">服务集合</param>
        /// <param name="setupAction">安装配置</param>
        public static JESAILoggingBuilder AddJESAILogging(this IServiceCollection services, Action<JESAILoggingOptions> setupAction)
        {
            services.TryAddSingleton<ILogFactory, LogFactory>();
            services.TryAddTransient(typeof(ILog<>), typeof(Log<>));
            services.TryAddTransient(typeof(ILoggerWrapper), typeof(LoggerWrapper));
            services.TryAddTransient(typeof(ILog), t => t.GetService<ILogFactory>()?.CreateLog("default") ?? NullLog.Instance);
            var options = new JESAILoggingOptions();
            setupAction(options);
            foreach (var serviceExtension in options.Extensions) 
                serviceExtension.AddServices(services);
            return new JESAILoggingBuilder(services);
        }
    }
}
